<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\User;

use App\Dao\BusinessDao;
use App\Dao\UserDao;
use App\Kernel\Util\UserJwtInstance;
use App\Service\Service;

use Hyperf\Di\Annotation\Inject;

/**
 * 渠道商服务
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Admin
 */
class ChannelService extends Service
{

    /**
     * @Inject
     * @var MoneyService
     */
    public $money_service;

    /**
     * @Inject
     * @var UserDao
     */
    public $user_dao;

    /**
     * @Inject
     * @var BusinessDao
     */
    public $business_dao;

    /**
     * 渠道商推广列表 - 商家
     * @param string $start_time
     * @return mixed
     */
    public function businessList(string $start_time)
    {
        $user = UserJwtInstance::instance()->build()->getUser();

        // 本月第一天
        $first = date('Y-m-01 0:0:0', strtotime($start_time));
        // 本月最后一天
        $last = date('Y-m-d 23:59:59', strtotime("$first +1 month -1 day"));

        $result = $this->money_service->money_dao->money([
            'user_id' => uid(),
            'price_source' => $this->money_service->money_dao::SOURCE_SCAN,
            'type' => $this->money_service->money_dao::INCOME_TYPE,
        ],strtotime($first),strtotime($last));

        $data['monthMoney'] = $this->money_service->money_dao->monthMoney([
            'user_id' => uid(),
            'price_source' => $this->money_service->money_dao::SOURCE_SCAN,
            'type' => $this->money_service->money_dao::INCOME_TYPE,
        ],strtotime($first),strtotime($last));

        $data['list'] = [];
        foreach ( $result as $key => $value ) {
            $data['list'][$key]['goods_name'] = $value['goods_name'];
            $data['list'][$key]['goods_pic'] = $value['goods_pic'];
            $data['list'][$key]['order_no'] = $value['order_no'];
            $data['list'][$key]['amount'] = filterMoney($value['amount'],4);
            $data['list'][$key]['num'] = filterMoney($value['num'],4);
            $data['list'][$key]['created_at'] = date('Y-m-d H:i',$value['created_at']);
        }

        return $data;
    }

    /**
     * 渠道商推广列表 - 会员
     * @param string $start_time
     * @return mixed
     */
    public function userList(string $start_time)
    {

        $user = UserJwtInstance::instance()->build()->getUser();

        // 本月第一天
        $first = date('Y-m-01 0:0:0', strtotime($start_time));
        // 本月最后一天
        $last = date('Y-m-d 23:59:59', strtotime("$first +1 month -1 day"));

        $result = $this->money_service->money_dao->money([
            'user_id' => uid(),
            'price_source' => $this->money_service->money_dao::SOURCE_WRITE,
            'type' => $this->money_service->money_dao::INCOME_TYPE,
        ],strtotime($first),strtotime($last));

        $data['monthMoney'] = $this->money_service->money_dao->monthMoney([
            'user_id' => uid(),
            'price_source' => $this->money_service->money_dao::SOURCE_WRITE,
            'type' => $this->money_service->money_dao::INCOME_TYPE,
        ],strtotime($first),strtotime($last));

        $data['list'] = [];
        foreach ( $result as $key => $value ) {
            $data['list'][$key]['goods_name'] = $value['goods_name'];
            $data['list'][$key]['goods_pic'] = $value['goods_pic'];
            $data['list'][$key]['order_no'] = $value['order_no'];
            $data['list'][$key]['amount'] = filterMoney($value['amount'],4);
            $data['list'][$key]['num'] = filterMoney($value['num'],4);
            $data['list'][$key]['created_at'] = date('Y-m-d H:i',$value['created_at']);
        }

        return $data;
    }

    /**
     * 渠道商推广列表 - 渠道商
     * @param string $start_time
     * @return mixed
     */
    public function channelList(string $start_time)
    {
        $user = UserJwtInstance::instance()->build()->getUser();

        // 本月第一天
        $first = date('Y-m-01 0:0:0', strtotime($start_time));
        // 本月最后一天
        $last = date('Y-m-d 23:59:59', strtotime("$first +1 month -1 day"));

        $result = $this->money_service->money_dao->money([
            'user_id' => uid(),
            'source' => $this->money_service->money_dao::SOURCE_CHANNEL,
            'price_source' => $this->money_service->money_dao::SOURCE_CHANNEL,
            'type' => $this->money_service->money_dao::INCOME_TYPE,
        ],strtotime($first),strtotime($last));

        $data['monthMoney'] = $this->money_service->money_dao->monthMoney([
            'user_id' => uid(),
            'source' => $this->money_service->money_dao::SOURCE_CHANNEL,
            'price_source' => $this->money_service->money_dao::SOURCE_CHANNEL,
            'type' => $this->money_service->money_dao::INCOME_TYPE,
        ],strtotime($first),strtotime($last));

        $data['monthCount'] = $this->money_service->money_dao->monthCount([
            'user_id' => uid(),
            'source' => $this->money_service->money_dao::SOURCE_CHANNEL,
            'price_source' => $this->money_service->money_dao::SOURCE_CHANNEL,
            'type' => $this->money_service->money_dao::INCOME_TYPE,
        ],strtotime($first),strtotime($last));

        $data['channel_total_price'] = $this->money_service->getMoneyBalanceByUser(uid()); //渠道商总金额

        $data['channel_count'] = $this->user_dao->count([
            'parent_id' => uid(),
            'group_id' => $this->user_dao::USER_CHANNEL_BUSINESS
        ]);

        $data['list'] = [];
        foreach ($result as $key => $value) {
            $data['list'][$key]['goods_name'] = $value['goods_name'];
            $data['list'][$key]['goods_pic'] = $value['goods_pic'];
            $data['list'][$key]['phone'] = $value['phone'];
            $data['list'][$key]['amount'] = $value['amount'];
            $data['list'][$key]['time'] = date('Y-m-d H:i', $value['created_at']);
            $data['list'][$key]['num'] = $value['num'];
            $data['list'][$key]['channel_phone'] = substr_replace($user->phone,'****',3,4);
        }


        return $data;
    }

    /**
     * 渠道商列表
     * @param int $type
     * @return mixed
     */
    public function list(int $type)
    {
        $result['channel_total_price'] = $this->money_service->getMoneyBalanceByUser(uid()); //渠道商总金额
        $result['commission_total_price'] = $this->money_service->getMoneyBalanceByCommission(uid());//渠道商佣金
        $result['member_total_price'] = $this->money_service->getMoneyBalanceByMember(uid());//渠道商会员
        $result['store_total_price'] = $this->money_service->getMoneyBalanceByStore(uid());//渠道商商家

        $result['channel_count'] = $this->user_dao->count([
            'parent_id' => uid(),
            'group_id' => $this->user_dao::USER_CHANNEL_BUSINESS
        ]);

        $result['user_count'] = $this->user_dao->count([
            'parent_id' => uid(),
            'group_id' => $this->user_dao::USER_MEMBER
        ]);

        $result['business_count'] = $this->business_dao->count([
            'parent_id' => uid(),
        ]);
        $result['list'] = [];
        if( $type === 1 ) {
            $result['list'] = $this->getMoneyByMember();
        } else if( $type === 2 ){
            $result['list'] = $this->getMoneyByStore();
        }

        return $result;
    }

    /**
     * 查询渠道商伞下会员
     *
     * @return mixed
     */
    public function getMoneyByMember()
    {
        $get = $this->user_dao->get([
            'parent_id' => uid(),
            'group_id' => $this->user_dao::USER_MEMBER
        ]);
        $result = [];
        foreach ( $get as $key => $value ) {
            $result[$key]['id'] = $value['id'];
            $result[$key]['name'] = $value['name'];
            $result[$key]['phone'] = $value['phone'];
            $result[$key]['avatar'] = $value['avatar'];
            $result[$key]['cumulatived_total_money'] = $value['cumulatived_money'] ?? 0.00;
            $result[$key]['cumulatived_money'] = $this->money_service->money_dao->num([
                    'user_id' => uid(),
                    'lower_id' => $value['id'],
                    'price_source' => 1,
                    'type' => 1,
                ]) ?? 0.00;
        }

        return $result;
    }

    /**
     * 查询渠道商伞下店铺
     * @return mixed
     */
    public function getMoneyByStore()
    {
        $get = $this->business_dao->get([
            'parent_id' => uid()
        ]);
        $result = [];
        foreach ( $get as $key => $value ) {
            $result[$key]['id'] = $value['id'];
            $result[$key]['name'] = $value['name'];
            $result[$key]['logo'] = $value['logo'];
            $result[$key]['phone'] = $value['phone'];
            $result[$key]['tel'] = $value['tel'];
            $result[$key]['total_stream_price'] = $this->money_service->getMoneyBalance($value['id']);
            $result[$key]['total_profit_price'] = $this->money_service->money_dao->num([
                    'user_id' => uid(),
                    'lower_id' => $value['user_id'],
                    'price_source' => 2,
                    'type' => 1,
                ]) ?? 0.00;
        }

        return $result;
    }
}